﻿using RBAC.Domain;
using RBAC.Domain.Dto;
using RBAC.Server.Interface;
using Sowrk03_28.Repository;
using Sowrk03_28.Repository.Interface;

namespace RBAC.Server.Impls
{
    public class UserServer : IUserServer
    {
        private readonly IUserRepository UserInfo;
        private readonly IRepository<ProVicnce> ProVicnce;
        private readonly IRepository<Citys> City;
        private readonly IRepository<District> District;
        private readonly IRepository<Roles> Roles;
        private readonly IRepository<UserRole> UserRole;

        public UserServer(IUserRepository userInfo,
            IRepository<ProVicnce> proVicnce,
            IRepository<Citys> city,
            IRepository<District> district,
            IRepository<Roles> Roles,
            IRepository<UserRole> UserRole
            )
        {
            UserInfo = userInfo;
            ProVicnce = proVicnce;
            City = city;
            District = district;
            this.Roles = Roles;
            this.UserRole = UserRole;
        }

        public async Task<List<ProVicnce>> GetProVicnces()
        {
            return await ProVicnce.AllAsync();
        }

        public Task<List<Citys>> GetCity(int id)
        {
            var info = City.GetValues(x => x.Pid == id);
            return info;
        }

        public async Task<List<District>> GetDistrict(int id)
        {
            var info = District.GetValues(x => x.Cid == id);
            return await info;
        }

        public async Task<int> AddUserInfo(UserInfo userInfo)
        {
            var only = await UserInfo.GetValues(x => x.Name == userInfo.Name);
            userInfo.CreatePeople = "张三";
            userInfo.CreateTime = DateTime.Now;
            userInfo.UpdPeople = null;
            userInfo.UpdTime = null;
            if (only.Count == 0)
            {
                //用户名不存在，可以添加
                UserInfo.CreateAsync(userInfo);
                return 1;
            }
            //用户名已存在
            return 0;
        }

        public async Task<(int totalCount, int pageCount, List<UserDto> list)> GetUserInfos(int pageIndex, int pageSize)
        {
            var info = await UserInfo.GetValues(x => !x.IsDel);
            var res = new List<UserDto>();
            int totalCount = info.Count();
            int pageCount = (int)Math.Ceiling(totalCount * 1.0 / pageSize);
            info = info.OrderByDescending(x => x.CreateTime).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            foreach (var item in info)
            {
                var role = UserRole.GetValues(x => x.UserId == item.Id).Result.Select(x => x.RoleId);
                if (role != null)
                {
                    var roleName = Roles.GetValues(x => role.Contains(x.Id)).Result.Select(x => x.Name).ToArray();
                    res.Add(new UserDto
                    {
                        Id = item.Id,
                        RoleName = String.Join(",", roleName),
                        Address = item.AddressDetail,
                        City = item.City,
                        Distrist = item.District,
                        Province = item.Province,
                        Name = item.Name,
                        Sex = item.Sex,
                        CreatePeople = item.CreatePeople,
                        CreateTime = item.CreateTime,
                        UpdPeople = item.UpdPeople,
                        UpdTime = item.UpdTime,
                        ProName = ProVicnce.GetAsync(item.Province).Result.Name,
                        CityName = City.GetAsync(item.City).Result.Name,
                        DistrictName = District.GetAsync(item.District).Result.Name,
                    });
                }
                else
                {
                    res.Add(new UserDto
                    {
                        Id = item.Id,
                        RoleName = null,
                        Address = item.AddressDetail,
                        City = item.City,
                        Distrist = item.District,
                        Province = item.Province,
                        Name = item.Name,
                        Sex = item.Sex,
                        CreatePeople = item.CreatePeople,
                        CreateTime = item.CreateTime,
                        UpdPeople = item.UpdPeople,
                        UpdTime = item.UpdTime,
                        ProName = ProVicnce.GetAsync(item.Province).Result.Name,
                        CityName = City.GetAsync(item.City).Result.Name,
                        DistrictName = District.GetAsync(item.District).Result.Name,
                    });
                }
            }
            return (totalCount, pageCount, res);
        }


        public async Task<UserInfo> GetUser(int id)
        {
            return await UserInfo.GetAsync(id);
        }

        public async Task<int> Upd(UserInfo userInfo)
        {
            var only = await UserInfo.GetValues(x => x.Name == userInfo.Name && x.Id != userInfo.Id);
            userInfo.UpdPeople = "李四";
            userInfo.UpdTime = DateTime.Now;
            if (only.Count != 0)
            {
                //除本人外有别的用户，不让修改
                return 0;
            }

            await UserInfo.UpdateAsync(userInfo);
            return 1;
        }

        public async Task<int> Del(int id)
        {
            var del = await UserInfo.GetAsync(id);
            if (del == null)
            {
                return 0;
            }
            del.IsDel = true;
            await UserInfo.UpdateAsync(del);
            return 1;
        }


        public int UserAdds(string? users)
        {
            return 1;
        }
    }
}

